import 'package:sow_app_core/sow_app_core.dart';

import '../usecase/usecase.dart';

import '../usecase/novel_usecase.dart';

abstract class NovelDetailController {
  final NovelDetailPresenter _presenter;
  final NovelRepository _novelRepository;
  NovelDetailController(this._presenter, this._novelRepository);

  init() => NovelDetailGetChapterList(_presenter, _novelRepository)(NoParams());

  Future<void> subscribe() async =>
      NovelDetailSubscribeUseCase(_presenter, _novelRepository)(NoParams());

  Future<void> addReadHistory() async =>
      IncreaseReadUseCase(_novelRepository)(_presenter.novelId);

  Future<void> addComment() async =>
      NovelDetailAddCommentUseCase(_presenter, _novelRepository)(NoParams());
}

class NovelDetailAddCommentUseCase extends UseCase<Future<void>, NoParams> {
  final NovelDetailPresenter _presenter;
  final NovelRepository _novelRepository;

  NovelDetailAddCommentUseCase(this._presenter, this._novelRepository);

  @override
  Future<void> call(NoParams params) async {
    try {
      await AddNovelCommentUseCase(_novelRepository)(
          NovelComment(_presenter.comment, '', int.parse(_presenter.novelId)));
      _presenter.commentList = await GetNovelCommentListUseCase(
          _novelRepository)(_presenter.novelId);
      _presenter.onComplete();
    } on ServerException catch (e) {
      _presenter.onError(e);
      rethrow;
    }
  }
}
